//
//  DomParser.m
//  CCB
//
//  Created by simon wang on 6/1/10.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "DomParser.h"


@implementation DomParser


// based on element name
+(int) findElementsWithName: (NSString *) elementName 
            inParentElement: (GDataXMLElement *) parentElement
                beRecursive: (BOOL) recursive
            withResultArray: (NSMutableArray *) resultArray
{
    NSArray *tempArray = [parentElement elementsForName: elementName];
    
    [resultArray addObjectsFromArray: tempArray];
    
    if (recursive)
    {
        for (GDataXMLElement *element in [parentElement children])
        {
            if([element kind] == GDataXMLElementKind)
            {
                [self findElementsWithName: elementName 
                           inParentElement: element 
                               beRecursive: recursive 
                           withResultArray: resultArray];
            }
        }   
    }
    
    return [resultArray count];
}

// based on attribute key-value
+(int) findElementsWithAttributeKey: (NSString *) attributeKey 
                  andAttributeValue: (NSString *) attributeValue
                    inParentElement: (GDataXMLElement *) parentElement
                        beRecursive: (BOOL) recursive
                    withResultArray: (NSMutableArray *) resultArray
{
    GDataXMLNode *tempNode = [parentElement attributeForName: attributeKey];
    
    // 匹配成功
    if([[tempNode stringValue] isEqualToString: attributeValue])
    {
        [resultArray addObject: parentElement];
    }
	
    if(recursive)
    {
        
        for (GDataXMLElement *element in [parentElement children])
        {
            if([element kind] == GDataXMLElementKind)
            {
                [self findElementsWithAttributeKey: attributeKey 
                                 andAttributeValue: attributeValue
                                   inParentElement: element
                                       beRecursive: recursive
                                   withResultArray: resultArray];
            }
        }
    }
    
    return [resultArray count];
}



@end
